package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfAccountInfo;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.entity.card.EntityXfMealInfo;
import com.ocom.common.entity.card.EntityXfUserParamConfig;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.payconfig.PerDiscountLimitRequest;
import com.ocom.common.request.web.ConsumeLimitConfigOnShowRequest;
import com.ocom.common.response.web.GetConsTypeResponse;
import com.ocom.common.vo.pay.ConsumeLimitVo;
import com.ocom.common.vo.pay.LimitConfigVo;
import com.ocom.common.vo.payconfig.PerDiscountLimitVo;
import com.ocom.common.vo.webapi.InLimitConfigVo;
import com.ocom.common.vo.webapi.LimitListVo;
import com.ocom.common.vo.webapi.ReConsumeLimitVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfUserParamConfig;
import com.ocom.payconfig.service.*;
import com.ocom.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class XfUserParamConfigServiceImpl extends ServiceImpl<IDaoXfUserParamConfig, EntityXfUserParamConfig> implements XfUserParamConfigService {


	@Autowired
	XfAccountTypeService xfAccountTypeService;

	@Autowired
	ConsTypeConfigService consTypeConfigService;

	@Autowired
	XfMealInfoService xfMealInfoService;

	@Autowired
	XfRecordProcessService xfRecordProcessService;

	@Autowired
	XfAccountInfoService xfAccountInfoService;

	@Resource
	RedisUtil redisUtil;
	// key：	com_id、acc_type_id、cons_type、meal_no

	/**
	 * 设置消费限制
	 * @param consumeLimitVo
	 * @return
	 */
	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
	public Boolean setConfig(Long companyId,ConsumeLimitVo consumeLimitVo) {

		for(LimitConfigVo limitConfigVo : consumeLimitVo.getLimitConfig()){
			UpdateWrapper<EntityXfUserParamConfig> updateWrapper=new UpdateWrapper<>();
			updateWrapper.set("times_limit",limitConfigVo.getTimesLimit());
			updateWrapper.set("amount_limit",limitConfigVo.getAmountLimit());
			updateWrapper.eq("acc_type_id",limitConfigVo.getAccountType());
			updateWrapper.eq("com_id",companyId);
			updateWrapper.eq("cons_type",consumeLimitVo.getConsType());
			updateWrapper.eq("meal_no",consumeLimitVo.getMealNo());
			updateWrapper.eq("limit_field",consumeLimitVo.getLimitField());
			updateWrapper.eq("limit_key",consumeLimitVo.getLimitKey());
			updateWrapper.eq("cons_time_zone",consumeLimitVo.getConsTimeZone());

			EntityXfUserParamConfig entityXfUserParamConfig=new EntityXfUserParamConfig();
			entityXfUserParamConfig.setComId(consumeLimitVo.getComId());
			entityXfUserParamConfig.setAccTypeId(limitConfigVo.getAccountType());
			entityXfUserParamConfig.setMealNo(consumeLimitVo.getMealNo());
			entityXfUserParamConfig.setConsType(consumeLimitVo.getConsType());
			entityXfUserParamConfig.setAmountLimit(limitConfigVo.getAmountLimit());
			entityXfUserParamConfig.setTimesLimit(limitConfigVo.getTimesLimit());
			entityXfUserParamConfig.setLimitField(consumeLimitVo.getLimitField());
			entityXfUserParamConfig.setLimitKey(consumeLimitVo.getLimitKey());
			entityXfUserParamConfig.setConsTimeZone(consumeLimitVo.getConsTimeZone());
			this.saveOrUpdate(entityXfUserParamConfig , updateWrapper);
//			String key = "xf_userparam_config" +
//					"#comId:" + consumeLimitVo.getComId() +
//					"#accTypeId:" + limitConfigVo.getAccountType() +
//					"#consType:" + consumeLimitVo.getConsType() +
//					"#mealNo:" + consumeLimitVo.getMealNo() +
//					"#timeZone:" + consumeLimitVo.getConsTimeZone() + "#";
//			redisUtil.del(key);
		}
		redisUtil.dels("xf_userparam_config#comId:" + consumeLimitVo.getComId());

		return true;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
	public ReConsumeLimitVo getConfig(Long companyId, ConsumeLimitConfigOnShowRequest request) {
		ReConsumeLimitVo reConsumeLimitVo=new ReConsumeLimitVo();
		reConsumeLimitVo.setAccountType(xfAccountTypeService.GetXFAccountType(companyId,request.getGrouping()));

		List<GetConsTypeResponse> entityConsTypeConfigList =  consTypeConfigService.getListByComId(companyId);

		reConsumeLimitVo.setConsTypeList(entityConsTypeConfigList);

//		Map<Integer , String > mapConsType =new HashMap<>();
//		if(entityConsTypeConfigList!=null && entityConsTypeConfigList.size()!=0){
//			mapConsType = entityConsTypeConfigList.stream()
//					.collect(Collectors.toMap(GetConsTypeResponse::getConsType, GetConsTypeResponse::getConsName));
//		}

		QueryWrapper<EntityXfUserParamConfig> queryWrapper=new QueryWrapper<>();
		queryWrapper.eq("com_id",companyId);
		queryWrapper.eq("limit_field",request.getLimitField());
		queryWrapper.eq("cons_time_zone",request.getConsTimeZone());
		if(request.getLimitField()==6 && (request.getLimitKey()!=null && request.getLimitKey()!=-1)){
			queryWrapper.eq("limit_key",request.getLimitKey());
		}
		List<EntityXfUserParamConfig> list= this.list(queryWrapper);

		if(list == null){
			list =new ArrayList<>();
		}

		List<EntityXfMealInfo> entityXfMealInfoList= xfMealInfoService.GetMealNameInfo(null,companyId,"","");
		Map<String , String > mapMealInfo =new HashMap<>();
		if(entityXfMealInfoList!=null && entityXfMealInfoList.size()!=0){
			mapMealInfo = entityXfMealInfoList.stream()
					.collect(Collectors.toMap(EntityXfMealInfo::getMealNo, EntityXfMealInfo::getMealName));

			for (EntityXfMealInfo entityXfMealInfo:entityXfMealInfoList){
				//初始化 返回数据
				LimitListVo limitListVo=new LimitListVo();
				limitListVo.setConsType(1);
				limitListVo.setConsTypeName("饭堂");
				limitListVo.setMealNo(Integer.valueOf(entityXfMealInfo.getMealNo()));
				limitListVo.setMealName(entityXfMealInfo.getMealName());

				List<InLimitConfigVo> limitConfig = new ArrayList<>();
				if(reConsumeLimitVo.getAccountType()!=null && reConsumeLimitVo.getAccountType().size()>0){
					for (EntityXfAccountType entityXfAccountType:reConsumeLimitVo.getAccountType()){
						InLimitConfigVo inLimitConfigVo=new InLimitConfigVo();
						inLimitConfigVo.setAccountType(entityXfAccountType.getAccTypeId());
						inLimitConfigVo.setAmountLimit(-1);
						inLimitConfigVo.setTimesLimit(-1);

						//查找值
						for (EntityXfUserParamConfig entityXfUserParamConfig : list){
							if(entityXfUserParamConfig.getConsType().compareTo(limitListVo.getConsType()) == 0 &&
									entityXfUserParamConfig.getMealNo().equals(limitListVo.getMealNo()) &&
									(Integer.valueOf(inLimitConfigVo.getAccountType())).compareTo(entityXfUserParamConfig.getAccTypeId()) == 0
							){
								inLimitConfigVo.setAmountLimit(entityXfUserParamConfig.getAmountLimit());
								inLimitConfigVo.setTimesLimit(entityXfUserParamConfig.getTimesLimit());
								break ;
							}
						}
						limitConfig.add(inLimitConfigVo);
					}
				}
				limitListVo.setLimitConfig(limitConfig);
				reConsumeLimitVo.getLimitList().add(limitListVo);
			}
		}



		for (GetConsTypeResponse entityConsTypeConfig  : entityConsTypeConfigList){
//			if(entityConsTypeConfig.getConsType()==1){
//				continue;
//			}
			LimitListVo limitListVo=new LimitListVo();
			limitListVo.setConsType(entityConsTypeConfig.getConsType());
			limitListVo.setConsTypeName(entityConsTypeConfig.getConsTypeName());
			limitListVo.setMealNo(500);
			limitListVo.setMealName("按天");
			List<InLimitConfigVo> limitConfig = new ArrayList<>();
			if(reConsumeLimitVo.getAccountType()!=null && reConsumeLimitVo.getAccountType().size()>0){
				for (EntityXfAccountType entityXfAccountType:reConsumeLimitVo.getAccountType()){
					InLimitConfigVo inLimitConfigVo=new InLimitConfigVo();
					inLimitConfigVo.setAccountType(entityXfAccountType.getAccTypeId());
					inLimitConfigVo.setAmountLimit(-1);
					inLimitConfigVo.setTimesLimit(-1);
					//查找值
					for (EntityXfUserParamConfig entityXfUserParamConfig : list){
						if(entityXfUserParamConfig.getConsType().compareTo(limitListVo.getConsType()) == 0 &&
								entityXfUserParamConfig.getMealNo().equals(limitListVo.getMealNo()) &&
								(Integer.valueOf(inLimitConfigVo.getAccountType())).compareTo(entityXfUserParamConfig.getAccTypeId()) == 0
						){
							inLimitConfigVo.setAmountLimit(entityXfUserParamConfig.getAmountLimit());
							inLimitConfigVo.setTimesLimit(entityXfUserParamConfig.getTimesLimit());
							break ;
						}
					}
					limitConfig.add(inLimitConfigVo);
				}
			}
			limitListVo.setLimitConfig(limitConfig);
			reConsumeLimitVo.getLimitList().add(limitListVo);


			if(request.getLimitField()==5 && entityConsTypeConfig.getConsType()==1){
				for (EntityXfMealInfo entityXfMealInfo:entityXfMealInfoList){
					if(Integer.valueOf(entityXfMealInfo.getMealNo())>=99){
						continue;
					}
					LimitListVo limitListVo5=new LimitListVo();
					limitListVo5.setConsType(entityConsTypeConfig.getConsType());
					limitListVo5.setConsTypeName(entityConsTypeConfig.getConsTypeName());
					limitListVo5.setMealNo(900+Integer.valueOf(entityXfMealInfo.getMealNo()));
					limitListVo5.setMealName("按月/"+entityXfMealInfo.getMealName());
					List<InLimitConfigVo> limitConfig5 = new ArrayList<>();
					if(reConsumeLimitVo.getAccountType()!=null && reConsumeLimitVo.getAccountType().size()>0){
						for (EntityXfAccountType entityXfAccountType:reConsumeLimitVo.getAccountType()){
							InLimitConfigVo inLimitConfigVo=new InLimitConfigVo();
							inLimitConfigVo.setAccountType(entityXfAccountType.getAccTypeId());
							inLimitConfigVo.setAmountLimit(-1);
							inLimitConfigVo.setTimesLimit(-1);
							//查找值
							for (EntityXfUserParamConfig entityXfUserParamConfig : list){
								if(entityXfUserParamConfig.getConsType().compareTo(limitListVo5.getConsType()) == 0 &&
										entityXfUserParamConfig.getMealNo().equals(limitListVo5.getMealNo()) &&
										(Integer.valueOf(inLimitConfigVo.getAccountType())).compareTo(entityXfUserParamConfig.getAccTypeId()) == 0
								){
									inLimitConfigVo.setAmountLimit(entityXfUserParamConfig.getAmountLimit());
									inLimitConfigVo.setTimesLimit(entityXfUserParamConfig.getTimesLimit());
									break ;
								}
							}
							limitConfig5.add(inLimitConfigVo);
						}
					}
					limitListVo5.setLimitConfig(limitConfig5);
					reConsumeLimitVo.getLimitList().add(limitListVo5);
				}
			}

			LimitListVo limitListVo1=new LimitListVo();
			limitListVo1.setConsType(entityConsTypeConfig.getConsType());
			limitListVo1.setConsTypeName(entityConsTypeConfig.getConsTypeName());
			limitListVo1.setMealNo(1000);
			limitListVo1.setMealName("按月");
			List<InLimitConfigVo> limitConfig1 = new ArrayList<>();
			if(reConsumeLimitVo.getAccountType()!=null && reConsumeLimitVo.getAccountType().size()>0){
				for (EntityXfAccountType entityXfAccountType:reConsumeLimitVo.getAccountType()){
					InLimitConfigVo inLimitConfigVo=new InLimitConfigVo();
					inLimitConfigVo.setAccountType(entityXfAccountType.getAccTypeId());
					inLimitConfigVo.setAmountLimit(-1);
					inLimitConfigVo.setTimesLimit(-1);
					//查找值
					for (EntityXfUserParamConfig entityXfUserParamConfig : list){
						if(entityXfUserParamConfig.getConsType().compareTo(limitListVo1.getConsType()) == 0 &&
								entityXfUserParamConfig.getMealNo().equals(limitListVo1.getMealNo()) &&
								(Integer.valueOf(inLimitConfigVo.getAccountType())).compareTo(entityXfUserParamConfig.getAccTypeId()) == 0
						){
							inLimitConfigVo.setAmountLimit(entityXfUserParamConfig.getAmountLimit());
							inLimitConfigVo.setTimesLimit(entityXfUserParamConfig.getTimesLimit());
							break ;
						}
					}
					limitConfig1.add(inLimitConfigVo);
				}
			}
			limitListVo1.setLimitConfig(limitConfig1);
			reConsumeLimitVo.getLimitList().add(limitListVo1);
		}

		return reConsumeLimitVo;
	}

	@Override
	@DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
	public PerDiscountLimitVo perDiscountLimit(Long companyId, PerDiscountLimitRequest request) {
		PerDiscountLimitVo perDiscountLimitVo = xfRecordProcessService.selectUserDiscount(companyId,request);


		/**
		 * 3.查询人员账户类型id ,@accTypeId
		 * select  acc_type_id  from  xf_account_info  where com_id=@comId  and  p_id =@pId
		 * 4.查询天优惠限制meal_no=500,月优惠限制meal_no=1000
		 * select  meal_no,amount_limit  from  xf_userparam_config
		 * where com_id=@comId  and  acc_type_id=@accTypeId
		 *  and limit_field=5 and meal_no in (500,1000) and cons_type=1 and cons_time_zone=0;
		 */

		EntityXfAccountInfo entityXfAccountInfo = xfAccountInfoService.getAccount(request.getComId(),request.getPId().intValue());

		if(entityXfAccountInfo==null){
			return perDiscountLimitVo;
		}

		QueryWrapper<EntityXfUserParamConfig> queryWrapper= new QueryWrapper<>();
		queryWrapper.eq("com_id",companyId);
		queryWrapper.eq("acc_type_id",entityXfAccountInfo.getAccTypeId());
		queryWrapper.eq("limit_field",5);
		queryWrapper.eq("meal_no",500);
		queryWrapper.eq("cons_type",request.getConsType());
		queryWrapper.eq("cons_time_zone",0);
		queryWrapper.last("limit 1");
		EntityXfUserParamConfig entityXfUserParamConfig = this.getOne(queryWrapper);
		if(entityXfUserParamConfig!=null){
			perDiscountLimitVo.setDayLimit(entityXfUserParamConfig.getAmountLimit());
		}

		QueryWrapper<EntityXfUserParamConfig> queryWrapper2= new QueryWrapper<>();
		queryWrapper2.eq("com_id",companyId);
		queryWrapper2.eq("acc_type_id",entityXfAccountInfo.getAccTypeId());
		queryWrapper2.eq("limit_field",5);
		queryWrapper2.eq("meal_no",1000);
		queryWrapper2.eq("cons_type",request.getConsType());
		queryWrapper2.eq("cons_time_zone",0);
		queryWrapper2.last("limit 1");
		EntityXfUserParamConfig entityXfUserParamConfig2 = this.getOne(queryWrapper2);
		if(entityXfUserParamConfig2!=null){
			perDiscountLimitVo.setMonthLimit(entityXfUserParamConfig2.getAmountLimit());
		}

		return perDiscountLimitVo;
	}
}
